import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, OneToMany, ManyToMany, JoinTable } from 'typeorm';
import { User } from '../user/user.entity';
import { Submission } from '../submission/submission.entity';
import { FieldType } from '../form-config/field-type.entity';
import { FormConfig } from '../form-config/form-config.entity';

@Entity()
export class Project {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ unique: true })
  name: string;

  @Column({ type: 'text', nullable: true })
  description: string;

  @Column({ nullable: true })
  mainImage: string;

  @ManyToMany(() => FieldType)
  @JoinTable({
    name: 'project_field_types',
    joinColumn: { name: 'project_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'field_type_id', referencedColumnName: 'id' }
  })
  fieldTypes: FieldType[];

  @Column({ type: 'text', nullable: true })
  qrCodeLink: string;

  @ManyToOne(() => User, (user) => user.projects)
  owner: User;

  @OneToMany(() => Submission, (submission) => submission.project)
  submissions: Submission[];

  @ManyToOne(() => FormConfig, { nullable: true })
  formConfig: FormConfig;
}